package cap.action;

import cap.bean.Article;
import cap.bean.Category;
import cap.bean.SysCategory;
import cap.bean.User;
import cap.dao.ArticleDao;
import cap.dao.CategoryDao;
import cap.dao.SysCategoryDao;
import cap.dao.impl.ArticleDaoImpl;
import cap.dao.impl.CategoryDaoImpl;
import cap.dao.impl.SysCategoryDaoImpl;
import cap.util.PageControl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/article")
public class ArticleServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private CategoryDao categoryDao;
    private SysCategoryDao sysCategoryDao;
    private ArticleDao articleDao;

    public ArticleServlet() {
        categoryDao=new CategoryDaoImpl();
        sysCategoryDao=new SysCategoryDaoImpl();
        articleDao=new ArticleDaoImpl();
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String method=request.getParameter("method");
        response.setContentType("text/html;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        if(method.equals("add")){//新建文章
            int userId = Integer.parseInt(request.getParameter("userId"));
            List<Category> cgList = categoryDao.getByUserId(userId);//根据userId查找用户的个人分类
            List<SysCategory> scgList = sysCategoryDao.getAllSysCategory();//获取所有系统分类
            request.setAttribute("cgList", cgList);
            request.setAttribute("scgList", scgList);
            request.getRequestDispatcher("front/AddArticle.jsp").forward(request, response);
        }else if(method.equals("save")){//保存文章
            int userId = Integer.parseInt(request.getParameter("userId"));
            String title = request.getParameter("title");
            String c=request.getParameter("sys_category");
            int scgId=1;
            if(c!=null) {
                scgId = Integer.parseInt(c);
            }
            int cgId = Integer.parseInt(request.getParameter("category"));
            String summary = request.getParameter("summary");
            String content = request.getParameter("content");
            int res = articleDao.insertArticle(title, userId, scgId, cgId, content, summary);
            if (res > 0) {	//添加新文章成功
                request.getSession().setAttribute("successMsg", "发布文章成功！");
            } else {
                request.getSession().setAttribute("errorMsg", "发布文章失败");
            }
            response.sendRedirect("article?method=manage&userId="+userId);
        }else if(method.equals("update")){//修改文章
            int artId = Integer.parseInt(request.getParameter("artId"));
            Article art = articleDao.getById(artId);
            List<SysCategory> scgList = sysCategoryDao.getAllSysCategory();//系统分类信息
            request.setAttribute("scgList", scgList);
            request.setAttribute("art", art);
            request.getRequestDispatcher("front/UpdateArticle.jsp").forward(request, response);
        }else if(method.equals("manage")){//博文管理
            int userId = Integer.parseInt(request.getParameter("userId"));
            //添加用于分页
            int total=articleDao.getByUserId(userId).size();
            String curPageStr = request.getParameter("curPage");
            int curPage=1;
            if(curPageStr!=null){
                curPage=Integer.parseInt(curPageStr);
            }
            PageControl pc = new PageControl(curPage, total);
            List<Article> artList=articleDao.getArticleByPageUserId(curPage,total, userId);
            System.out.println(artList);
            pc.setList(articleDao.getByUserId(userId));
            request.setAttribute("curPage", pc.getCurPage());
            request.setAttribute("totalPages", pc.getTotalPages());
            request.setAttribute("artList", pc.getList());
            request.getRequestDispatcher("front/ArticleManage.jsp").forward(request, response);
        }else if(method.equals("delete")){//删除文章
            int artId = Integer.parseInt(request.getParameter("artId"));
            int userId = Integer.parseInt(request.getParameter("userId"));
            int res = articleDao.deleteArtical(artId);
            if (res > 0) {	//删除成功
                request.getSession().setAttribute("deleSuccMsg", "删除成功！");
            } else {
                request.getSession().setAttribute("deleErrorMsg", "删除失败！");
            }
            response.sendRedirect("article?method=manage&userId="+userId);
        }else if(method.equals("saveupdate")){//保存修改文章
            int artId = Integer.parseInt(request.getParameter("artId"));
            String title = (String)request.getParameter("title");
            int userId = Integer.parseInt(request.getParameter("userId"));
            int scgId = Integer.parseInt(request.getParameter("sys_category"));
            int cgId = Integer.parseInt(request.getParameter("category"));
            String content = (String)request.getParameter("content");
            String summary = (String)request.getParameter("summary");
            int res = articleDao.updateArticle(artId, title, userId, scgId, cgId, content, summary);
            if (res > 0) {	//更新成功
                request.getSession().setAttribute("successMsg", "修改文章成功！");
            } else {
                request.getSession().setAttribute("errorMsg", "修改文章失败");
            }
            response.sendRedirect("article?method=update&artId="+artId);
        }
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
